home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / 3d / irit50src.lha / irit5 / prsr_lib / cagdread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-29  |  12.3 KB  |  276 lines

  1. /******************************************************************************
  2. * CagdRead.c - Generic Curve/Surface reading from files.              *
  3. *******************************************************************************
  4. * Written by Gershon Elber, July. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #include "prsr_loc.h"
  8.  
  9. /*****************************************************************************
  10. * DESCRIPTION:                                                               M
  11. * Reads from a file curve(s).                             M
  12. *   If error is detected in reading the file, ErrStr is set to a string      M
  13. * describing the error and Line to the line it occured in file.             M
  14. *   If no error is detected *ErrStr = NULL.                     M
  15. *                                                                            *
  16. * PARAMETERS:                                                                M
  17. *   FileName:     To read the curve from.                              M
  18. *   ErrStr:       Will be initialized if an error has occured.               M
  19. *   ErrLine:      Line number in file FileName of the error, if occured.     M
  20. *                                                                            *
  21. * RETURN VALUE:                                                              M
  22. *   CagdCrvStruct *: The read curve, or NULL if an error occured.            M
  23. *                                                                            *
  24. * KEYWORDS:                                                                  M
  25. *   CagdCrvReadFromFile, files, read                                         M
  26. *****************************************************************************/
  27. CagdCrvStruct *CagdCrvReadFromFile(char *FileName, char **ErrStr, int *ErrLine)
  28. {
  29.     int Handler;
  30.     FILE *f;
  31.     IPTokenType Token;
  32.     char StringToken[LINE_LEN];
  33.  
  34.     if ((f = fopen(FileName, "r")) == NULL) {
  35.     *ErrStr = "File not found";
  36.     *ErrLine = 0;
  37.     return NULL;
  38.     }
  39.     Handler = IritPrsrOpenStreamFromFile(f, TRUE,
  40.                      IritPrsrSenseBinaryFile(FileName),
  41.                      FALSE);
  42.  
  43.     while ((Token = _IPGetToken(Handler, StringToken)) != IP_TOKEN_EOF &&
  44.        Token != IP_TOKEN_OPEN_PAREN);
  45.  
  46.     /* We found beginning of definition - read one: */
  47.     if (_IPGetToken(Handler, StringToken) != IP_TOKEN_CURVE ||
  48.     (Token = _IPGetToken(Handler, StringToken)) == IP_TOKEN_EOF) {
  49.         *ErrStr = "CURVE key words expected";
  50.     *ErrLine = _IPStream[Handler].LineNum;
  51.     return NULL;
  52.     }
  53.  
  54.     IritPrsrCloseStream(Handler, TRUE);
  55.  
  56.     switch (Token) {
  57.     case IP_TOKEN_BEZIER:
  58.         return BzrCrvReadFromFile(FileName, ErrStr, ErrLine);
  59.     case IP_TOKEN_BSPLINE:
  60.         return BspCrvReadFromFile(FileName, ErrStr, ErrLine);
  61.     default:
  62.         *ErrStr = "BSPLINE or BEZIER Token expected";
  63.         *ErrLine = _IPStream[Handler].LineNum;
  64.         return NULL;
  65.     }
  66. }
  67.  
  68. /*****************************************************************************
  69. * DESCRIPTION:                                                               M
  70. * Reads from a file surface(s).                             M
  71. *   If error is detected in reading the file, ErrStr is set to a string      M
  72. * describing the error and Line to the line it occured in file.             M
  73. *   If no error is detected *ErrStr = NULL.                     M
  74. *                                                                            *
  75. * PARAMETERS:                                                                M
  76. *   FileName:     To read the surface from.                                 M
  77. *   ErrStr:       Will be initialized if an error has occured.               M
  78. *   ErrLine:      Line number in file FileName of the error, if occured.     M
  79. *                                                                            *
  80. * RETURN VALUE:                                                              M
  81. *   CagdSrfStruct *: The read surface, or NULL if an error occured.          M
  82. *                                                                            *
  83. * KEYWORDS:                                                                  M
  84. *   CagdSrfReadFromFile, files, read                                         M
  85. *****************************************************************************/
  86. CagdSrfStruct *CagdSrfReadFromFile(char *FileName, char **ErrStr, int *ErrLine)
  87. {
  88.     int Handler;
  89.     FILE *f;
  90.     IPTokenType Token;
  91.     char StringToken[LINE_LEN];
  92.  
  93.     if ((f = fopen(FileName, "r")) == NULL) {
  94.     *ErrStr = "File not found";
  95.     return NULL;
  96.     }
  97.     Handler = IritPrsrOpenStreamFromFile(f, TRUE,
  98.                      IritPrsrSenseBinaryFile(FileName),
  99.                      FALSE);
  100.  
  101.     while ((Token = _IPGetToken(Handler, StringToken)) != IP_TOKEN_EOF &&
  102.        Token != IP_TOKEN_OPEN_PAREN);
  103.  
  104.     /* We found beginning of definition - read one: */
  105.     if (_IPGetToken(Handler, StringToken) != IP_TOKEN_SURFACE ||
  106.     (Token = _IPGetToken(Handler, StringToken)) == IP_TOKEN_EOF) {
  107.         *ErrStr = "SURFACE key words expected";
  108.     *ErrLine = _IPStream[Handler].LineNum;
  109.     return NULL;
  110.     }
  111.  
  112.     IritPrsrCloseStream(Handler, TRUE);
  113.  
  114.     switch (Token) {
  115.     case IP_TOKEN_BEZIER:
  116.         return BzrSrfReadFromFile(FileName, ErrStr, ErrLine);
  117.     case IP_TOKEN_BSPLINE:
  118.         return BspSrfReadFromFile(FileName, ErrStr, ErrLine);
  119.     default:
  120.         *ErrStr = "BSPLINE or BEZIER Token expected";
  121.         *ErrLine = _IPStream[Handler].LineNum;
  122.         return NULL;
  123.     }
  124. }
  125.  
  126. /*****************************************************************************
  127. * DESCRIPTION:                                                               M
  128. * Reads from a stream a curve.                             M
  129. *    It is assumed prefix "[CURVE" has already been read. This is useful for M
  130. * a global parser which invokes this routine to read from a stream several   M
  131. * times as a parent controller.                              M
  132. *   For exactly this reason, the given stream descriptor is NOT closed in    M
  133. * the end.                                     M
  134. *   If error is found in reading the stream, ErrStr is set to a string       M
  135. * describing it and ErrLine to line it occured in stream relative to         M
  136. * begining of curve.                                 M
  137. *   If no error is detected *ErrStr is set to NULL.                 M
  138. *                                                                            *
  139. * PARAMETERS:                                                                M
  140. *   Handler:      A handler to the open stream.                     M
  141. *   ErrStr:       Will be initialized if an error has occured.               M
  142. *   ErrLine:      Line number in strea of the error, if occured.         M
  143. *                                                                            *
  144. * RETURN VALUE:                                                              M
  145. *   CagdCrvStruct *: The read curve, or NULL if an error occured.            M
  146. *                                                                            *
  147. * KEYWORDS:                                                                  M
  148. *   CagdCrvReadFromFile2, files, read, stream                                M
  149. *****************************************************************************/
  150. CagdCrvStruct *CagdCrvReadFromFile2(int Handler, char **ErrStr, int *ErrLine)
  151. {
  152.     char StringToken[LINE_LEN];
  153.  
  154.     switch (_IPGetToken(Handler, StringToken)) {
  155.     case IP_TOKEN_BEZIER:
  156.         return BzrCrvReadFromFile2(Handler, TRUE, ErrStr, ErrLine);
  157.     case IP_TOKEN_BSPLINE:
  158.         return BspCrvReadFromFile2(Handler, TRUE, ErrStr, ErrLine);
  159.     default:
  160.         *ErrStr = "BSPLINE or BEZIER Token expected";
  161.         *ErrLine = _IPStream[Handler].LineNum;
  162.         return NULL;
  163.     }
  164. }
  165.  
  166. /*****************************************************************************
  167. * DESCRIPTION:                                                               M
  168. * Reads from a stream a surface.                         M
  169. *    It is assumed prefix "[SURFACE" has already been read. This is useful   M
  170. * for a global parser which invokes this routine to read from a stream       M
  171. * several times as a parent controller.                          M
  172. *   For exactly this reason, the given stream descriptor is NOT closed in    M
  173. * the end.                                     M
  174. *   If error is found in reading the stream, ErrStr is set to a string       M
  175. * describing it and ErrLine to line it occured in stream relative to         M
  176. * begining of surface.                                 M
  177. *   If no error is detected *ErrStr is set to NULL.                 M
  178. *                                                                            *
  179. * PARAMETERS:                                                                M
  180. *   Handler:      A handler to the open stream.                     M
  181. *   ErrStr:       Will be initialized if an error has occured.               M
  182. *   ErrLine:      Line number in stream of the error, if occured.         M
  183. *                                                                            *
  184. * RETURN VALUE:                                                              M
  185. *   CagdSrfStruct *: The read surface, or NULL if an error occured.          M
  186. *                                                                            *
  187. * KEYWORDS:                                                                  M
  188. *   CagdSrfReadFromFile2, files, read, stream                                M
  189. *****************************************************************************/
  190. CagdSrfStruct *CagdSrfReadFromFile2(int Handler, char **ErrStr, int *ErrLine)
  191. {
  192.     char StringToken[LINE_LEN];
  193.  
  194.     switch (_IPGetToken(Handler, StringToken)) {
  195.     case IP_TOKEN_BEZIER:
  196.         return BzrSrfReadFromFile2(Handler, TRUE, ErrStr, ErrLine);
  197.     case IP_TOKEN_BSPLINE:
  198.         return BspSrfReadFromFile2(Handler, TRUE, ErrStr, ErrLine);
  199.     default:
  200.         *ErrStr = "BSPLINE or BEZIER Token expected";
  201.         *ErrLine = _IPStream[Handler].LineNum;
  202.         return NULL;
  203.     }
  204. }
  205.  
  206. /*****************************************************************************
  207. * DESCRIPTION:                                                               *
  208. * Routine to read from input stream the    following [ATTR ...] [ATTR ...].     *
  209. * Note the '[' was already read.                         *
  210. * Current supported attributes: None.                         *
  211. * Returns NULL if O.k., otherwise string describing the error.             *
  212. *                                                                            *
  213. * PARAMETERS:                                                                *
  214. *   Handler:      A handler to the open stream.                     *
  215. *                                                                            *
  216. * RETURN VALUE:                                                              *
  217. *   char *:       Always NULL                                                *
  218. *****************************************************************************/
  219. char *_IPGetCurveAttributes(int Handler)
  220. {
  221.     IPTokenType i;
  222.     char StringToken[LINE_LEN];
  223.  
  224.     do {
  225.     switch (_IPGetToken(Handler, StringToken)) {
  226.         default:
  227.         while ((i = _IPGetToken(Handler, StringToken)) !=
  228.                                IP_TOKEN_CLOSE_PAREN &&
  229.                i != IP_TOKEN_EOF);
  230.         if (i == IP_TOKEN_EOF)
  231.             return "EOF detected in middle of attribute.";
  232.         break;
  233.     }
  234.     }
  235.     while (_IPGetToken(Handler, StringToken) == IP_TOKEN_OPEN_PAREN);
  236.  
  237.     _IPUnGetToken(Handler, StringToken);
  238.  
  239.     return NULL;
  240. }
  241. /*****************************************************************************
  242. * DESCRIPTION:                                                               *
  243. * Routine to read from input stream the    following [ATTR ...] [ATTR ...].     *
  244. * Note the '[' was already read.                         *
  245. * Current supported attributes: None.                         *
  246. * Returns NULL if O.k., otherwise string describing the error.             *
  247. *                                                                            *
  248. * PARAMETERS:                                                                *
  249. *   Handler:      A handler to the open stream.                     *
  250. *                                                                            *
  251. * RETURN VALUE:                                                              *
  252. *   char *:       Always NULL                                                *
  253. *****************************************************************************/
  254. char *_IPGetSurfaceAttributes(int Handler)
  255. {
  256.     IPTokenType i;
  257.     char StringToken[LINE_LEN];
  258.  
  259.     do {
  260.     switch (_IPGetToken(Handler, StringToken)) {
  261.         default:
  262.         while ((i = _IPGetToken(Handler, StringToken)) !=
  263.                                IP_TOKEN_CLOSE_PAREN &&
  264.                i != IP_TOKEN_EOF);
  265.         if (i == IP_TOKEN_EOF)
  266.             return "EOF detected in middle of attribute.";
  267.         break;
  268.     }
  269.     }
  270.     while (_IPGetToken(Handler, StringToken) == IP_TOKEN_OPEN_PAREN);
  271.  
  272.     _IPUnGetToken(Handler, StringToken);
  273.  
  274.     return NULL;
  275. }
  276.